package com.heima.controller;


import com.heima.common.PageResponse;
import com.heima.entity.TbSku;
import com.heima.mapper.TbSkuMapper;
import com.heima.pojo.vo.TbSkuVo;
import com.heima.service.ITbSkuService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-07
 */
@RestController
@Slf4j
@RequestMapping("/api/vm-service")
public class TbSkuController {

    @Autowired
    private ITbSkuService iTbSkuService;

    @Autowired
    private TbSkuMapper tbSkuMapper;

    @PostMapping("/sku")
    public Boolean add(@RequestBody TbSku tbSku){
      log.info("新增商品,{}",tbSku);
      return iTbSkuService.add(tbSku);
    }

    @GetMapping("/sku/search")
    public PageResponse<TbSkuVo> selectTbSku(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "skuName",required = false) String skuName){
        log.info("商品搜索,{当前页},{每页记录},{名称}",pageIndex,pageSize,skuName);
        return iTbSkuService.selectTbSku(pageIndex,pageSize,skuName);
    }

    @PutMapping("/sku/:skuId")
    public Boolean updateGoods(@RequestBody TbSku tbSku){
        TbSku tbSku1 = new TbSku();
        tbSku1.setUpdateTime(LocalDateTime.now());
        tbSku1.setUpdateTime(LocalDateTime.now());
        tbSku1.setSkuName(tbSku.getSkuName());
        tbSku1.setSkuImage(tbSku.getSkuImage());
        tbSku1.setClassId(tbSku.getClassId());
        tbSku1.setUnit(tbSku.getUnit());
        tbSku1.setPrice(tbSku.getPrice());
        tbSku1.setBrandName(tbSku.getBrandName());
        tbSku1.setSkuId(Long.parseLong(String.valueOf(tbSku.getSkuId())));

        int row = tbSkuMapper.updateById(tbSku1);
        if (row>0) {
            return true;
        }
        return false;
    }

}
